package com.anrongtec.zcpt.authentication;


import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.art.base.util.AppManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 统一认证广播接收器
 *
 * @author dut
 */
public class AuthorizeReceiver extends BroadcastReceiver {


    private static List<AuthorizeLoginObserver> authorizeLoginObservers;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.e("arg1",arg1.getAction());
//        if (arg1.getAction().equals(arg0.getPackageName())) {
////        if (arg1.getAction().equals("com.xdja.unifyauthorize.ACTION_LOGOUT")){
//            notifyObserver();
//        } else

//        Uri uri = Uri.parse("content://com.xdja.app.pj/" + arg0.getPackageName());
//        ContentResolver contentResolver = arg0.getContentResolver();
//        contentResolver.delete(uri, null, null);

        Log.e("arg0",arg0.getPackageName());
            if (arg1.getAction().equals(JXClientConstans.BillInfo.LOGOUT_SYSTEM)) {
            String packageName = arg1.getStringExtra("packageName");
            //如果包名不是自己，说明是其他应用发启登出，需要退程序
            if (packageName.equals(arg0.getPackageName())) {
                //退出
                AppManager.getAppManager().finishAllActivity();
                Intent intent=new Intent();
//                intent.setClassName("com.xdja.unifyauthorize", "com.xdja.unifyauthorize.LoginActivity");
                intent.setComponent(new ComponentName("com.xdja.unifyauthorize","com.xdja.unifyauthorize.LoginActivity"));
//                arg0.startActivity(intent);
//          JXClientApplication.getApplication().exitApp(true);

            }
        }
    }



    private void notifyObserver() {
        if (authorizeLoginObservers != null) {

            for (int i = 0; i < authorizeLoginObservers.size(); i++) {
                AuthorizeLoginObserver observer = authorizeLoginObservers.get(i);
                if (observer != null) {
                    observer.loginSuccess();
                }
            }
        }

    }

    /**
     * 注册观察者
     *
     * @param observer observer
     */
    public static void registerObserver(AuthorizeLoginObserver observer) {
        if (authorizeLoginObservers == null) {
            authorizeLoginObservers = new ArrayList<AuthorizeLoginObserver>();
        }
        authorizeLoginObservers.add(observer);
    }

    /**
     * 注销观察者
     *
     * @param observer observer
     */
    public static void removeRegisterObserver(AuthorizeLoginObserver observer) {
        if (authorizeLoginObservers != null) {
            authorizeLoginObservers.remove(observer);
        }
    }

}
